using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 燃烧Buff - 持续造成火焰伤害
/// </summary>
public class BurnBuff : BuffBase
{
    private float m_TickInterval = 1.0f; // 每秒造成一次伤���
    private float m_LastTickTime;
    private long m_DamagePerTick;

    public BurnBuff(BuffData buffData, Entity caster, Entity target) : base(buffData, caster, target)
    {
        // 从BuffData中获取伤害值，这里假设BurnData有DamagePerTick字段
        if (buffData is BurnData burnData)
        {
            m_DamagePerTick = burnData.DamagePerTick;
            m_TickInterval = burnData.TickInterval;
        }
    }

    protected override void OnStart()
    {
        base.OnStart();
        m_LastTickTime = 0f;
        
        // 播放燃烧特效
        if (BuffData.Effect != null)
        {
            var effect = GameObject.Instantiate(BuffData.Effect, Target.transform);
            effect.transform.localPosition = Vector3.zero;
        }
        
        Logger.Log($"{Target.name} 开始燃烧，每秒造成 {m_DamagePerTick} 点伤���");
    }

    protected override void OnTick(float dt)
    {
        base.OnTick(dt);
        
        // 检查是否到了造成伤害的时���
        if (Time.time - m_LastTickTime >= m_TickInterval)
        {
            ApplyBurnDamage();
            m_LastTickTime = Time.time;
        }
    }

    private void ApplyBurnDamage()
    {
        if (Target != null && !Target.HealthComponent.IsDead)
        {
            // 造成火焰伤害
            Target.HealthComponent.TakeDamage(m_DamagePerTick);
            
            // 可以在这里添加伤害数字显示、特效等
            Logger.Log("BurnBuff", $"{Target.name} 受到燃烧伤害: {m_DamagePerTick}");
            
            // 触发伤害事件
            var args = ReferencePool.Acquire<EntityTakeDamageEventArgs>();
            args.Entity = Target;
            args.Damage = m_DamagePerTick;
            args.Attacker = Caster;
            EventHelper.Fire(this, args);
        }
    }

    protected override void OnFinish()
    {
        base.OnFinish();
        Logger.Log("BurnBuff", $"{Target.name} 燃烧效果结束");
    }

    protected override void OnRemove()
    {
        base.OnRemove();
        // 清理特效
        var effects = Target.GetComponentsInChildren<ParticleSystem>();
        foreach (var effect in effects)
        {
            if (effect.name.Contains("Burn"))
            {
                GameObject.Destroy(effect.gameObject);
            }
        }
    }

    public override bool IsCanMerge()
    {
        return true; // 燃烧效果可以叠加，刷新持续时���
    }
}
